package com.longshihan.gril.controller;

import com.longshihan.gril.entity.Gril;
import com.longshihan.gril.entity.GrilReposity;
import com.longshihan.gril.entity.Result;
import com.longshihan.gril.service.GrilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.*;
import java.util.List;

@RestController
public class GrilController {

    @Autowired
    private GrilReposity mGrilRep;

    private GrilService grilService;

    @GetMapping(value = "/grils")
    public List<Gril> getList() {

        return mGrilRep.findAll();
    }

  /*  @PostMapping(value = "/grils")
    public Gril grilAdd(@RequestParam("cupSize") String cupSize,
                        @RequestParam("age") Integer age) {

        Gril gril = new Gril();
        gril.setAge(age);
        gril.setCupSize(cupSize);
        return mGrilRep.save(gril);
    }*/


    @PostMapping(value = "/grils")
    public Result<Gril> grilAdd1(@Valid Gril gril, BindingResult bindingResult) {
        Result result = new Result();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setData(null);
            result.setMsg(bindingResult.getFieldError().getDefaultMessage());
            return result;
        }
        gril.setAge(gril.getAge());
        gril.setCupSize(gril.getCupSize());
        result.setCode(0);
        result.setMsg("成功");
        result.setData(mGrilRep.save(gril));
        return result;
    }

    @GetMapping(value = "/grils/{id}")
    public Gril getGrilId(@PathVariable("id") Integer id) {

        return mGrilRep.findOne(id);
    }

    @PostMapping(value = "/grils/two")
    public void insertTwo() {
        grilService.insertTwo();
    }


    @PostMapping(value = "/grils/one")
    public void insertGril(@Valid Gril gril) {
        gril.setAge(gril.getAge());
        gril.setCupSize(gril.getCupSize());
        mGrilRep.save(gril);
    }
    @PostMapping(value = "/uploadfile")
    public void upload(@RequestParam("file") MultipartFile file) {
        System.out.println("------------:------------");
        if (!file.isEmpty()) {
            try {
                // 这里只是简单例子，文件直接输出到项目路径下。
                // 实际项目中，文件需要输出到指定位置，需要在增加代码处理。
                // 还有关于文件格式限制、文件大小限制，详见：中配置。
                System.out.println(":" + file.getOriginalFilename());
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(file.getOriginalFilename())));
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("------------:------------`");

            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("------------:------------1");

            }
        }
    }

}
